/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 * This converted is responsible for making subString from strings. The strings 
 * are taken as parameter throught faces config, cause this class implements Converter
 * faces iterface. 
 * @author Tom
 * @version 1.0.0. beta
 */
public class StringConverter implements Converter {

    private final int maxLengthOfString = 30;

    public StringConverter() {
    }
        /**
     * #{@inheritDoc }.
     * From parama string is making substring. Test to string null object are provided and
     * then is made substring from string. Lenght of substring is specify in 
     * <code>maxLengthOfString</code> which is final variable in this class.
     * @param fc faces context
     * @param string string from which will be made substring
     * @param uic component
     * @return substring from string in object paramLenght of substring is specify in 
     * <code>maxLengthOfString</code> which is final variable in this class.
     */
    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        String line = string;
        if (string == null) {
            return line;
        } else if (line.length() > maxLengthOfString) {
            line = line.substring(0, maxLengthOfString);
            line = line.concat("...");
        }
        return line;
    }
    /**
     * #{@inheritDoc }
     * From object is get string to convert. Test to null object are provided and
     * then is made substring from string. Lenght of substring is specify in 
     * <code>maxLengthOfString</code> which is final variable in this class.
     * @param fc faces context
     * @param o obejct from which will be taken the string to substring
     * @param uic component 
     * @return substring from string in object paramLenght of substring is specify in 
     * <code>maxLengthOfString</code> which is final variable in this class.
     */
    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        String line = (String) o;
        if (o == null) {
            return line;
        } else if (line.length() > maxLengthOfString) {
            line = line.substring(0, maxLengthOfString);
            line = line.concat("...");
        }
        return line;
    }
}
